import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { SetStorageStatus } from '@/api/app/setting';
import { useMessage } from '@/hooks/web/useMessage';

// tab的list
export const settingList = [
  {
    key: '1',
    name: '基础配置',
    component: 'StorageSetting',
  },
  {
    key: '2',
    name: '七牛云储存',
    component: 'CloudSetting',
  },
  {
    key: '3',
    name: '阿里云储存',
    component: 'CloudSetting',
  },
  {
    key: '4',
    name: '腾讯云储存',
    component: 'CloudSetting',
  },
  {
    key: '5',
    name: '京东云储存',
    component: 'CloudSetting',
  },
  {
    key: '6',
    name: '华为云储存',
    component: 'CloudSetting',
  },
  {
    key: '7',
    name: '天翼云储存',
    component: 'CloudSetting',
  },
];

// 云储存配置 list
export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 150,
    editRow: false,
    defaultHidden: true,
  },
  {
    title: '储存空间名称',
    dataIndex: 'name',
    width: 150,
    editRow: true,
    editRule: true,
  },
  {
    title: '区域',
    dataIndex: '_region',
    width: 150,
    align: 'center',
    editRow: true,
    editRule: true,
  },
  {
    title: '空间域名',
    width: 350,
    dataIndex: 'domain',
    align: 'center',
    editRow: true,
    editRule: true,
  },
  {
    title: '使用状态',
    dataIndex: 'status',
    width: 100,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '开启',
        unCheckedChildren: '关闭',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          SetStorageStatus(record.id)
            .then(() => {
              record.status = newStatus;
              createMessage.success('修改成功');
            })
            .catch(() => {})
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: '_create_time',
    align: 'center',
    width: 150,
    editRow: true,
  },
  {
    title: '更新时间',
    dataIndex: '_update_time',
    align: 'center',
    width: 150,
    editRow: false,
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'appid',
    label: 'APPID',
    component: 'Input',
    componentProps: {
      placeholder: '请输入APPID',
    },
    required: true,
  },
  {
    field: 'accessKey',
    label: 'AccessKeyId',
    component: 'Input',
    componentProps: {
      placeholder: '请输入AccessKeyId',
    },
    required: true,
  },
  {
    field: 'secretKey',
    label: 'AccessKeySecret',
    component: 'Input',
    componentProps: {
      placeholder: '请输入AccessKeySecret',
    },
    required: true,
  },
  {
    field: 'name',
    label: '空间名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入空间名称',
    },
    required: true,
  },
  {
    field: 'region',
    label: '空间区域',
    component: 'TreeSelect',
    componentProps: {
      placeholder: '请输入空间区域',
      fieldNames: {
        label: 'label',
        key: 'value',
        value: 'value',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    field: 'acl',
    label: '读写权限',
    component: 'RadioButtonGroup',
    defaultValue: 'public-read',
    componentProps: {
      options: [
        { label: '公共读(推荐)', value: 'public-read' },
        { label: '公共读写', value: 'public-read-write' },
      ],
    },
    required: true,
  },
];

export const settingSchema: FormSchema[] = [
  {
    field: 'type',
    label: 'Type',
    component: 'Input',
    show: false,
  },
  {
    field: 'accessKey',
    label: 'AccessKeyId',
    component: 'Input',
    componentProps: {
      placeholder: '请输入AccessKeyId',
    },
    required: true,
  },
  {
    field: 'secretKey',
    label: 'AccessKeySecret',
    component: 'Input',
    componentProps: {
      placeholder: '请输入AccessKeySecret',
    },
    required: true,
  },
  {
    field: 'appid',
    label: 'APPID',
    component: 'Input',
    componentProps: {
      placeholder: '请输入APPID',
    },
    required: true,
  },
  {
    field: 'storageRegion',
    label: 'Region',
    component: 'Input',
    componentProps: {
      placeholder: '请输入Region',
    },
    required: true,
  },
];
